package com.changgou.web.gateway.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Service;

/**
 * @author kfzhao
 * @version 1.0
 * @className AuthService
 * @description
 * @date 2022/1/2 21:11
 */
@Service
public class AuthService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 从request中获取cookie的值
     *
     * @param request
     * @return java.lang.String
     * @throws
     * @description
     * @author kfzhao
     * @date 2022/1/2 21:13
     */
    public String getJtiFromCookie(ServerHttpRequest request) {
        HttpCookie cookie = request.getCookies().getFirst("uid");
        if (cookie != null) {
            return cookie.getValue();
        }
        return null;
    }

    /**
     * 从redis中获取jwt
     *
     * @param jti
     * @return java.lang.String
     * @throws
     * @description
     * @author kfzhao
     * @date 2022/1/2 21:31
     */
    public String getJwtFromRedis(String jti) {
        String jwt = stringRedisTemplate.boundValueOps(jti).get();
        return jwt;
    }
}
